package com.gxa.meiye.admin.shop.controller;

import com.gxa.meiye.admin.shop.service.MainService;
import com.gxa.meiye.core.common.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @Author cc
 * @PackageName meiye
 * @Package com.gxa.meiye.admin.shop.controller
 * @Date 2022/7/22 18:48
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("shopMain")
@Api(tags = "首页接口")
public class AdminMainController {
    @Autowired
    private MainService mainService;
    @ApiOperation("进入主页默认渲染今日信息")
    @GetMapping("today")
    public Map getToday(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String todayTime = sdf.format(date);
        JsonResult jsonResult = new JsonResult();
        Integer payNum = mainService.queryPayNum(2,todayTime);
        Integer checkNum = mainService.queryCheckNum(4,todayTime);
        Integer refundNum = mainService.queryRefundNum(8,todayTime);
        BigDecimal pay = mainService.getPay(2,todayTime);
        BigDecimal income = mainService.getIncome(4,todayTime);
        BigDecimal refund = mainService.getRefund(8,todayTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("payNum",payNum);
        map.put("checkNum",checkNum);
        map.put("refundNum",refundNum);
        map.put("pay",pay);
        map.put("income",income);
        map.put("refund",refund);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(map);
        return jsonResult.getMap();
    }

    @ApiOperation("渲染7日收入概况信息")
    @GetMapping("sevenDayMoney")
    public Map getSevenDayMoney(){
        JsonResult jsonResult =new JsonResult();
        long nowTime = Calendar.getInstance().getTimeInMillis();
        long sevenDays = 7L;
        long sevenDaysAgo = nowTime - 86400000*sevenDays;
        BigDecimal sevenDaysAgoPay = mainService.SevenDaysAgoPay(2, nowTime, sevenDaysAgo);
        BigDecimal SevenDaysAgoIncome = mainService.SevenDaysAgoIncome(4, nowTime, sevenDaysAgo);
        BigDecimal SevenDaysAgoRefund = mainService.SevenDaysAgoRefund(8, nowTime, sevenDaysAgo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("sevenDaysAgoPay",sevenDaysAgoPay);
        map.put("SevenDaysAgoIncome",SevenDaysAgoIncome);
        map.put("SevenDaysAgoRefund",SevenDaysAgoRefund);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(map);
        return jsonResult.getMap();
    }


    @ApiOperation("渲染30日收入概况信息")
    @GetMapping("thirtyDaysMoney")
    public Map getThirtyDaysMoney(){
        JsonResult jsonResult =new JsonResult();
        long nowTime = Calendar.getInstance().getTimeInMillis();
        long thirtyDays = 30L;
        long thirtyDaysAgo = nowTime - 86400000*thirtyDays;
        System.out.println("当前时间"+nowTime);
        System.out.println("30天前时间"+thirtyDaysAgo);
        BigDecimal thirtyDaysAgoPay = mainService.ThirtyDaysAgoPay(2, nowTime, thirtyDaysAgo);
        BigDecimal thirtyDaysAgoIncome = mainService.ThirtyDaysAgoIncome(4, nowTime, thirtyDaysAgo);
        BigDecimal thirtyDaysAgoRefund = mainService.ThirtyDaysAgoRefund(8, nowTime, thirtyDaysAgo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("thirtyDaysAgoPay",thirtyDaysAgoPay);
        map.put("thirtyDaysAgoIncome",thirtyDaysAgoIncome);
        map.put("thirtyDaysAgoRefund",thirtyDaysAgoRefund);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(map);
        return jsonResult.getMap();
    }


    @ApiOperation("渲染7日订单概况信息")
    @GetMapping("sevenDayOder")
    public Map getSevenDayOder(){
        JsonResult jsonResult =new JsonResult();
        long nowTime = Calendar.getInstance().getTimeInMillis();
        long sevenDays = 7L;
        long sevenDaysAgo = nowTime - 86400000*sevenDays;
        Integer sevenDaysAgoPayNum = mainService.SevenDaysAgoPayNum(2, nowTime, sevenDaysAgo);
        Integer sevenDaysAgoCheckNum = mainService.SevenDaysAgoCheckNum(4, nowTime, sevenDaysAgo);
        Integer selectSevenDaysAgoRefundNum = mainService.SevenDaysAgoRefundNum(8, nowTime, sevenDaysAgo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("sevenDaysAgoPayNum",sevenDaysAgoPayNum);
        map.put("sevenDaysAgoCheckNum",sevenDaysAgoCheckNum);
        map.put("selectSevenDaysAgoRefundNum",selectSevenDaysAgoRefundNum);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(map);
        return jsonResult.getMap();
    }


    @ApiOperation("渲染30日订单概况信息")
    @GetMapping("thirtyDaysOder")
    public Map getThirtyDaysOder(){
        JsonResult jsonResult =new JsonResult();
        long nowTime = Calendar.getInstance().getTimeInMillis();
        long thirtyDays = 30L;
        long thirtyDaysAgo = nowTime - 86400000*thirtyDays;
        Integer thirtyDaysAgoPayNum = mainService.ThirtyDaysAgoPayNum(2, nowTime, thirtyDaysAgo);
        Integer thirtyDaysAgoCheckNum = mainService.ThirtyDaysAgoCheckNum(4, nowTime, thirtyDaysAgo);
        Integer thirtyDaysAgoRefundNum = mainService.ThirtyDaysAgoRefundNum(8, nowTime, thirtyDaysAgo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("thirtyDaysAgoPayNum",thirtyDaysAgoPayNum);
        map.put("thirtyDaysAgoCheckNum",thirtyDaysAgoCheckNum);
        map.put("thirtyDaysAgoRefundNum",thirtyDaysAgoRefundNum);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(map);
        return jsonResult.getMap();
    }

}
